using System;
using System.Threading.Tasks;

namespace MiniAspNetCore
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await new WebHostBuilder() // 实例化web主机建造者
                .UseHttpListener() // 使用http监听服务
                .Configure(app => app // 配置中间件
                    .Use(MiddleWare1)
                    .Use(MiddleWare2)
                    .Use(MiddleWare3))
                .Build() // 构建web主机
                .StartAsync(); // 运行web主机
        }

        static RequestDelegate MiddleWare1(RequestDelegate next) => async context => 
        {
            await context.Response.WriteAsync($"{nameof(MiddleWare1)} Start\n");
            await next(context);
            await context.Response.WriteAsync($"{nameof(MiddleWare1)} End\n");
        };

        static RequestDelegate MiddleWare2(RequestDelegate next) => async context =>
        {

            await context.Response.WriteAsync($"{nameof(MiddleWare2)} Start\n");
            await next(context);
            await context.Response.WriteAsync($"{nameof(MiddleWare2)} End\n");
        };

        static RequestDelegate MiddleWare3(RequestDelegate next) => async context =>
        {
            await context.Response.WriteAsync($"{nameof(MiddleWare3)} Process\n");
        };
    }
}

// 参考资料
// https://www.bilibili.com/video/BV1LJ411z7FR/?spm_id_from=333.788.recommend_more_video.2
